<%@page import="org.wabson.alfresco.translations.JarBundleLoader"%>
<%@page import="org.wabson.alfresco.translations.BundleLoaderFactory"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.wabson.alfresco.translations.MessageBundle"%>
<%@page import="org.wabson.alfresco.translations.BundleLoadingException"%>
<%
MessageBundle mb;
String packageName, packageURL, downloadURL;
String filePath = request.getParameter("f");

WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

if (filePath == null || filePath.equals(""))
{
    throw new ServletException("Must specify a bundle file name");
}
if (!filePath.endsWith(".properties") && !filePath.endsWith(".txt"))
{
    throw new ServletException("Bundle file name must end with .properties or .txt suffix");
}

if (request.getParameter("j") != null && !request.getParameter("j").equals(""))
{
	 packageName = request.getParameter("j");
	 packageURL = "/details?j=" + packageName;
	 downloadURL = "/download?j=" + packageName + "&f=" + filePath;
    
    if (!packageName.endsWith(".jar"))
    {
        throw new ServletException("Bundle name must end with .jar suffix");
    }
    JarBundleLoader loader = ((BundleLoaderFactory) springContext.getBean("bundle-loader-factory")).createLoader(packageName, "");
    try
    {
        mb = loader.load(filePath);
    }
    catch (BundleLoadingException e)
    {
        throw new ServletException("Unable to load message bundle", e);
    }
}
else if (request.getParameter("k") != null && !request.getParameter("k").equals(""))
{
	 packageName = request.getParameter("k");
    packageURL = "/details?k=" + request.getParameter("k");
    downloadURL = "/download?k=" + request.getParameter("k") + "&f=" + filePath;
    
    try
    {
    	JarBundleLoader loader = ((BundleLoaderFactory) springContext.getBean("langpack-loader-factory")).createLoader(packageName, "");
        mb = loader.load(filePath);
        packageName = loader.getFileName();
    }
    catch (BundleLoadingException e)
    {
        throw new ServletException("Unable to load message bundle", e);
    }
}
else
{
    throw new ServletException("Must specify either a JAR file or blob key");
}
String fileName = filePath;
if (fileName.lastIndexOf('/') > 0)
{
	fileName = filePath.substring(fileName.lastIndexOf('/') + 1, fileName.length());
}
%>


<%@page import="java.util.Properties"%>
<%@page import="org.apache.commons.lang3.StringEscapeUtils"%><html>
<head>
<title>Bundle <%= fileName %></title>
<link rel="stylesheet" type="text/css" href="/assets/screen.css" />
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-340797-7']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>
<body>
<div class="header">
<p class="title"><a href="/">Alfresco I18N Tools</a></p>
</div>

<div class="body">
<h1>Bundle <%= fileName %></h1>
<p>From bundle package <tt><a href="<%= packageURL %>"><%= packageName %></a></tt> under <tt><%= filePath %></tt></p>
<pre class="properties">
<%
boolean isContinuation = false;
for(String line : mb.getLines())
{
	String s = line;
	line = line.trim();
	if (!isContinuation)
	{
		if (line.startsWith("#"))
		{
			s = "<span class=\"comment\">" + line.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\\\"", "&quot;") + "</span>";
		}
		else if (line.indexOf('=') > 0)
		{
			String[] parts = line.split("=", 2);
			String k = parts[0].trim(), v = parts[1].trim();
			s = "<span class=\"key\">" + k.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\\\"", "&quot;") + "</span><span class=\"sep\">=</span><span class=\"val\">" + v.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\\\"", "&quot;") + "</span>";
		}
	}
	else
	{
		s = "<span class=\"val\">" + line.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\\\"", "&quot;") + "</span>";
	}
	isContinuation = (line.endsWith("\\"));
%><%= StringEscapeUtils.unescapeJava(s) %>
<%
}
%>
</pre>
<p>Download original <a href="<%= downloadURL %>"><%= filePath %></a></p>
</div>
</body>
</html>